/*******************************************************************************
** CopyRight (c) 2005-2008 SKY-MOBI Ltd. All rights reserved.
** Filename:  adWin.c
** Creator:   yuzhifeng
** Version:   0.0
** Date:      
** Description:
 *******************************************************************************/

#include "mrc_base.h"
#include "mpsFuncPool.h"

#include "ad_i.h"
#include "sgl_window.h"
#include "smp.h"
#include "smp_label.h"
#include "string.h"
#include "res.h"
#include "sgl_string.h"
//#include "ad_string.h"
#include "util_funcs.h"
#include "ad_manager.h"
#include "ad_ui.h"

#define AD_INTERVAL   10*1000
enum {
	AD_WIN_TEXT,
	AD_WIN_PIC
};

static int32 adTimer;
static HWND pLabel;

static AD_LIST* pAd;

HWND hAdWin;
int32 func_adProc;

int32 AD_init(const char* param)
{
	pAd = AD_GetList();
	return MR_SUCCESS;
}

void AD_TimerCb(int32 data)
{
	HWND hWnd = (HWND)data;
	if(!pAd || pAd->num == 0)
		return;
	
	if(pAd->item[pAd->currIdx].type == AD_CONTENT_TXT)
	{
		SMP_Label_SetContent(pLabel, NULL, pAd->item[pAd->currIdx].u.text, 500);
		SMP_Label_StartAutoScroll(pLabel, 1);
	}
	else
		SMP_Label_SetContent(pLabel, &(pAd->item[pAd->currIdx].u.pic), NULL, 0);

	pAd->currIdx++;
	if(pAd->currIdx >= pAd->num) pAd->currIdx = 0;

	SGL_UpdateWindow(hWnd);
}

void AD_InsertAdWin(HWND hWnd)
{
	if(!hAdWin) {
		hAdWin = SMP_CreateWindow(func_adProc,
			0,  _HEIGHT(hWnd)-SMP_TOOLBAR_HEIGHT-ADWIN_HEIGHT, _WIDTH(hWnd), ADWIN_HEIGHT,
			ADWIN_WID, WS_TABSTOP | WS_FOCUSCHILD, 0);
		//AD_TimerCb((int32)hAdWin);
	}

	SGL_RemoveChildWindow(hAdWin);
	SGL_AddChildWindow(hWnd, hAdWin);
}

LRESULT BottomAD_WinProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{

	//HWND hControl;

	switch(Msg)
	{

	case WM_CREATE:
	{
		// TODO: just for test
		AD_init(NULL);
		//mrc_timerStart(adTimer, AD_INTERVAL, (int32)hWnd, AD_TimerCb, TRUE);
		pLabel = SMP_CreateWindow(SMP_LABEL_WNDPROC, 
								0, 0, _WIDTH(hWnd), _HEIGHT(hWnd), 
								AD_WIN_TEXT, SMP_LABELS_AUTOSCROLL | SMP_LABELS_HCENTER | WS_TABSTOP, 0);
		SGL_AddChildWindow(hWnd, pLabel);
		// TODO: just for test
		if(pAd && pAd->num > 0)
			SMP_Label_SetContent(pLabel, &(pAd->item[0].u.pic), NULL, 0);

		return 0;
	}
	case WM_DESTROY:
	{
		if(adTimer)
			mrc_timerDelete(adTimer);
		adTimer = 0;
		break;
	}
	case WM_SHOW:
	{
		/*
		if(!adTimer) {
			adTimer = mrc_timerCreate();
			AD_TimerCb((int32)hWnd);
		}
		mrc_timerStart(adTimer, AD_INTERVAL, (int32)hWnd, AD_TimerCb, TRUE);
		*/
		break;
	}
	case WM_HIDE:
	{
		if(adTimer)
			mrc_timerStop(adTimer);
		break;
	}
	case WM_PAINT:
	{
		SSint16 x=0, y=0;

		SGL_WindowToScreen(hWnd, &x, &y);
		GAL_FillBox(NULL, x, y, _WIDTH(hWnd), _HEIGHT(hWnd), _BGCOLOR(hWnd));
		break;
	}
	case WM_KEYDOWNREPEAT:
	case WM_KEYDOWN:
	{
		switch(wParam)
		{
		case MR_KEY_DOWN:
			//SGL_FocusNext(_PARENT(hWnd), 1);
			SGL_FocusNext(SGL_GetTopWindow(hWnd), 1);
			return 1;
		case MR_KEY_UP:
			//SGL_FocusNext(_PARENT(hWnd), 0);
			SGL_FocusNext(SGL_GetTopWindow(hWnd), 0);
			return 1;
		}
		return 0;
	}
	case WM_KEYUP:
	{
		switch(wParam)
		{
		case MR_KEY_LEFT:
			
			return 1;
		case MR_KEY_RIGHT:
			
			return 1;
		}
		return 0;
	}

	}
	return 0;
   }


